/*
 * vim:et sts=4 sw=4 cindent:
 * $Id$
 */

import com.digg.fdk.model.*;
import com.digg.services.*;
import com.digg.services.response.UserResponse;
import com.digg.services.response.UsersResponse;
import mx.controls.*;

API.appKey = 'http://diggflashdevkit.googlecode.com/svn/trunk/as2/examples/friendbrowser';

Stage.align = 'TL';
Stage.scaleMode = 'noScale';

var user:User;
var friends:Array;
var usernameInput:TextInput;
var loadUserButton:mx.controls.Button;
var userClip:MovieClip;
var friendsList:List = userClip.friendsList;
var loadFriendButton:mx.controls.Button = userClip.loadFriendButton;

var oldUserButtonLabel:String = loadUserButton.label;
var oldFriendButtonLabel:String = loadFriendButton.label;

usernameInput.addEventListener('enter', onButtonClicked);
loadUserButton.addEventListener('click', onButtonClicked);
loadFriendButton.addEventListener('click', onFriendSelected);

userClip._visible = false;

/* 
 * uncomment this if you want the friend to be loaded without
 * having to click the "load friend" button
 */
// friendsList.addEventListener('change', onFriendSelected);

function onButtonClicked(event:Object):Void
{
    var username:String = usernameInput.text;
    if (username)
    {
        loadUser(username, event.target);
        loadUserButton.label = "loading...";
        loadUserButton.enabled = false;
    }
    else
    {
        Alert.show("You need to enter a username.", "Whoops!");
    }
}

function onFriendSelected(event:Object):Void
{
    var username:String = friendsList.selectedItem.label;
    if (username)
    {
        loadUser(username);
    }
    else
    {
        Alert.show("You need to select a friend first.", "Whoops!");
    }
}

function loadUser(username:String, button:mx.controls.Button):Void
{
    // trace('loading user: "' + username + '"...');
    var userResponse:UserResponse = API.getUser(username);
    userResponse.addLoadListener(onUserLoaded);
}

function onUserLoaded(event:Object):Void
{
    var response:UserResponse = event.target;
    if (event.success)
    {
        var user:User = response.getItem();
        
        // Alert.show('got user: "' + user.name + '", items: ' + items);        
        // trace('got user: "' + user.name + '"');
        
        showUser(user);
    }
    else
    {
        var error:ResponseError = response.getError();
        // trace(error);
        Alert.show(error.text, 'Error');
    }
    loadUserButton.label = oldUserButtonLabel;
    loadUserButton.enabled = true;
}

function showUser(user:User):Void
{
    userClip._visible = true;
    userClip.nameField.text = user.name;
    userClip.registeredField.text = 'registered: ' + user.registered.format();
    userClip.icon.loadMovie(user.icon);
    
    friendsList.removeAll();
    loadFriends(user);
}

function loadFriends(user:User):Void
{
    var friendsResponse:UsersResponse = API.getUserFriends(user);
    loadFriendButton.label = "loading...";
    loadFriendButton.enabled = false;

    friendsResponse.addLoadListener(onFriendsLoaded);
}

function onFriendsLoaded(event:Object):Void
{
    var response:UsersResponse = event.target;
    if (event.success)
    {
        var friends:Array = response.getItems();
        // trace('loaded ' + friends.length + ' friends');
        for (var i:Number = 0; i < friends.length; i++)
        {
            var friend:User = friends[i];
            friendsList.addItem(friend.name);
        }
    }
    else
    {
        var error:ResponseError = response.getError();
        // trace(error);
        Alert.show(error.text, 'Error');
    }
    loadFriendButton.label = oldFriendButtonLabel;
    loadFriendButton.enabled = true;
}
